package com.voicechat.firstmet.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.voicechat.firstmet.R;
import com.voicechat.firstmet.base.BaseAdapter;
import com.voicechat.firstmet.bean.CoummunityNotice;
import com.voicechat.firstmet.client.OssConfig;
import com.voicechat.firstmet.utils.GlideLoadUtils;
import com.voicechat.firstmet.utils.Utils;

import java.text.ParseException;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @ProjectName: FirstMet
 * @Package: com.voicechat.firstmet.adapter
 * @ClassName: SystemAndOfficialAdapter
 * @Description: java类作用描述
 * @Author: ljp
 * @CreateDate: 2018/11/17 13:43
 * @Version: 1.0
 */
public class SystemAndOfficialAdapter extends BaseAdapter<CoummunityNotice.PageBean.ListBean> {
    private String type;
    private OnItemClick onItemClick;
    private String TAG = "SystemAndOfficialAdapter";

    public void setOnItemClick(OnItemClick onItemClick) {
        this.onItemClick = onItemClick;
    }

    /**
     * @param context activity_imessage
     * @param tlist   集合
     * @param mLayout 主布局
     */
    public SystemAndOfficialAdapter(Context context, List<CoummunityNotice.PageBean.ListBean> tlist,
                                    int mLayout,String type) {
        super(context, tlist, mLayout);
        this.type = type;
    }

    @Override
    protected RecyclerView.ViewHolder getHeaderViewHolder(View view) {
        return null;
    }

    @Override
    protected RecyclerView.ViewHolder getItemViewHolder(View view) {
        if(type.equals("Official")){
            return new ItemViewHolderOfficial(view);
        }else {
            return new ItemViewHolderSystem(view);
        }
    }

    @Override
    protected RecyclerView.ViewHolder getFooterViewHolder(View view) {
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
        if(viewHolder instanceof ItemViewHolderSystem){//系统消息
            ItemViewHolderSystem itemViewHolderSystem = (ItemViewHolderSystem) viewHolder;
            CoummunityNotice.PageBean.ListBean listBean = tlist.get(position);
            if(type.equals("Steam")){
                itemViewHolderSystem.tvTextMsg.setVisibility(View.VISIBLE);
                itemViewHolderSystem.tvTextMsg.setText(listBean.getContent());
            }else {
                itemViewHolderSystem.tvTextMsg.setVisibility(View.GONE);
                itemViewHolderSystem.relaAddFriends.setVisibility(View.VISIBLE);
                Log.i(TAG, "onBindViewHolder: ---------"+listBean.toString());

                if(listBean.getSign() == 0){//处理标记 0未处理(默认) 1同意  2拒绝
                    itemViewHolderSystem.linearState.setVisibility(View.VISIBLE);
                    itemViewHolderSystem.tvTextAccept.setText("接受");
                    itemViewHolderSystem.tvTextNikeName.setText(listBean.getNickName()+"请求加你为隐友");
                }else if(listBean.getSign() == 1){
                    itemViewHolderSystem.linearState.setVisibility(View.GONE);
                    itemViewHolderSystem.tvTextNikeName.setText("您已经同意了"+listBean.getNickName()+"的隐友请求");
                }else {
                    itemViewHolderSystem.linearState.setVisibility(View.GONE);
                    itemViewHolderSystem.tvTextNikeName.setText("您已经拒绝了"+listBean.getNickName()+"的隐友请求");
                }
                itemViewHolderSystem.tvTextReject.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        onItemClick.setOnItemClick(position,listBean,1);//1拒绝
                    }
                });
                itemViewHolderSystem.tvTextAccept.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        onItemClick.setOnItemClick(position,listBean,2);//1接受
                    }
                });
            }
            Date date = null;
            if (!TextUtils.isEmpty(listBean.getCreateDate())) {
                try {
                    date = Utils.stringToDate(listBean.getCreateDate(), "yyyy-MM-dd HH:mm:ss");
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                if (Utils.getIsCurYearFromTime(date)) {//如果是今年，就不显示年
                    itemViewHolderSystem.tvTime.setText(Utils.dateToString(date, "MM-dd HH:mm"));
                } else {
                    itemViewHolderSystem.tvTime.setText(Utils.dateToString(date, "yyyy-MM-dd HH:mm:ss"));
                }
            }
        }else if(viewHolder instanceof ItemViewHolderOfficial){//官方通知
            ItemViewHolderOfficial itemViewHolderOfficial = (ItemViewHolderOfficial) viewHolder;
            CoummunityNotice.PageBean.ListBean listBean = tlist.get(position);
            if(!TextUtils.isEmpty(listBean.getUrl())) {
                if (!listBean.getUrl().equals(itemViewHolderOfficial.imageIcon.getTag(R.id.image_icon))) {
                    // 加载图片
                    itemViewHolderOfficial.imageIcon.setTag(R.id.image_icon, listBean.getUrl());
                    GlideLoadUtils.getInstance().glideLoad(context, OssConfig.OSS_HTTP +listBean.getUrl(),
                            itemViewHolderOfficial.imageIcon);
                }
            }
            itemViewHolderOfficial.tvTitle.setText(listBean.getTitle());
        }
    }
    //系统消息页面
    class ItemViewHolderSystem extends BaseViewHolder{
        @BindView(R.id.tv_time)
        TextView tvTime;
        @BindView(R.id.tv_text_msg)
        TextView tvTextMsg;
        @BindView(R.id.rela_add_friends)
        RelativeLayout relaAddFriends;
        @BindView(R.id.tv_nike_name)
        TextView tvTextNikeName;
        @BindView(R.id.tv_reject)
        TextView tvTextReject;
        @BindView(R.id.tv_accept)
        TextView tvTextAccept;
        @BindView(R.id.linear_sign)
        LinearLayout linearState;

        public ItemViewHolderSystem(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }
    //官方通知页面
    class ItemViewHolderOfficial extends BaseViewHolder{
        @BindView(R.id.image_icon)
        ImageView imageIcon;
        @BindView(R.id.tv_title)
        TextView tvTitle;
        public ItemViewHolderOfficial(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }

    public interface OnItemClick{
        void setOnItemClick(int position,CoummunityNotice.PageBean.ListBean listBean,int flag);

    }
}
